<?php

namespace app\admin\controller;

use think\Db;

class Authority extends Common{

	public function index(){
		return $this->fetch();
	}
	//权限数据接口
	public function dataList(){
		$limit = request()->param('limit')?:20;
		if ($this->admin_info['data_uid']) {
			$admis = Db::table('tp_admin')->where('pid',$this->uid)->column('id');
			$admis[] = $this->uid;
			$data  = Db::table('tp_admin')->where('type',1)->where('id','in',$admis)->paginate($limit);
		} else {
			$data  = Db::table('tp_admin')->where('type',1)->paginate($limit);
		}
		
		$data  = $data->toArray();
		return $this->tableData($data);
	}
	
	//编辑权限
	public function edit(){
		if (request()->method() == 'POST') {
			$uid       = request()->param('id');
			$nav       = request()->param('nav/a');
			$other     = request()->param('other/a');
			$authority = Db::table('tp_admin')->where('id',$uid)->value('authority');
			if($authority == 0){
				return ['code'=>1,'msg'=>'超级管理员不可编辑'];
			} else {
				Db::startTrans();
				try{
					//已有权限
					$has = Db::table('tp_admin_authority')->where('uid',$uid)->where('other',null)->column('a_id');
					if (!empty($has) && !empty($nav)) {
						//删除权限
						$de = array_diff($has, $nav);
						//新增权限
						$ad = array_diff($nav, $has);
					} elseif(!empty($has) && empty($nav)) {
						$de = $has;
						$ad = [];
					} elseif (empty($has) && !empty($nav)) {
						$de = [];
						$ad = $nav;
					} elseif(empty($has) && empty($nav)) {
						$de = [];
						$ad = [];
					}
					if (!empty($de)) {
						Db::table('tp_admin_authority')->where('uid',$uid)->where('a_id','in',$de)->delete();
					}
					if (!empty($ad)) {
						foreach ($ad as $key => $val) {
							$c_id = Db::table('tp_admin_nav')->where('id',$val)->value('pid');
							Db::table('tp_admin_authority')->insert([
								'uid'=>$uid,
								'c_id'=>$c_id,
								'a_id'=>$val
							]);
						}
					}
					//其他权限
					$other_has = Db::table('tp_admin_authority')->where('uid',$uid)->column('other');
					if (!empty($other_has) && !empty($other)) {
						//删除权限
						$other_de = array_diff($other_has, $other);
						//新增权限
						$other_ad = array_diff($other, $other_has);
					} elseif(!empty($other_has) && empty($other)) {
						$other_de = $other_has;
						$other_ad = [];
					} elseif (empty($other_has) && !empty($other)) {
						$other_de = [];
						$other_ad = $other;
					} elseif(empty($other_has) && empty($other)) {
						$other_de = [];
						$other_ad = [];
					}
					if (!empty($other_de)) {
						Db::table('tp_admin_authority')->where('uid',$uid)->where('other','in',$other_de)->delete();
					}
					if (!empty($other_ad)) {
						foreach ($other_ad as $key => $val) {
							Db::table('tp_admin_authority')->insert([
								'uid'=>$uid,
								'other'=>$val,
							]);
						}
					}
					Db::commit();
					return ['code'=>0,'msg'=>'success'];
				} catch(\Exception $e) {
	                //回滚事务
	                Db::rollback();
	                return ['code'=>1,'msg'=>'更新失败'.$e->getMessage()];
	            }

			}
		} else {
			$uid = request()->param('uid');
			$auth = Db::table('tp_admin')->where('id',$uid)->find();
			$authority = $auth['authority'];
			$auth2 = Db::table('tp_admin')->where('id',$this->uid)->find();
			$authority2 = $auth['authority'];
			if ($auth2['data_uid'] == 0) {
				$nav = Db::table('tp_admin_nav')->where('pid',0)->where('is_use',1)->order('sort asc')->select();
			} else {
				$cids = Db::table('tp_admin_authority')->where('uid',$this->uid)->group('c_id')->column('c_id');
				$nav = Db::table('tp_admin_nav')->where('pid',0)->where('id','in',$cids)->where('is_use',1)->order('sort asc')->select();
			}
			
			foreach ($nav as $key => $val) {
				if ($auth2['data_uid'] == 0) {
					$nav[$key]['nav_list'] = Db::table('tp_admin_nav')->where('pid',$val['id'])->where('is_use',1)->select();
				} else {
					$aids = Db::table('tp_admin_authority')->where('uid',$this->uid)->group('a_id')->column('a_id');
					$nav[$key]['nav_list'] = Db::table('tp_admin_nav')->where('pid',$val['id'])->where('id','in',$aids)->where('is_use',1)->select();
				}
				
				foreach ($nav[$key]['nav_list'] as $k => $v) {
					if($authority == 0) {
						$nav[$key]['nav_list'][$k]['has'] = 1;
					} else {
						$res = Db::table('tp_admin_authority')->where('uid',$uid)->where('c_id',$val['id'])->where('a_id',$v['id'])->find();
						if ($res) {
							$nav[$key]['nav_list'][$k]['has'] = 1;
						} else {
							$nav[$key]['nav_list'][$k]['has'] = 0;
						}
					}
				}
			}
			//其他权限
			if($authority == 0) {
				$bb = 1;
				$user_edit = 1;
			} else {
				$bb = Db::table('tp_admin_authority')->where('uid',$uid)->where('other',1)->find();
				$bb = $bb?1:0;
				$user_edit = Db::table('tp_admin_authority')->where('uid',$uid)->where('other',2)->find();
				$user_edit = $user_edit?1:0;
			}
			$other = [
				 'id'=>0,
				 'title'=>'其他',
				 'nav_list'=>[
				// 	['id'=>1,'title'=>'拨币','has'=>$bb],
				// 	['id'=>2,'title'=>'用户编辑','has'=>$user_edit]
				 ]
			];
			//var_dump($nav);die();
			$this->assign('id',$uid);
			$this->assign('other',$other);
			$this->assign('nav',$nav);
			return $this->fetch();
		}
	}
}